package com.my.common.util.date;

import lombok.experimental.UtilityClass;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * 时间工具类
 *
 * @author: marshal
 * @date: 2023-06-30 17:16
 */
@UtilityClass
public class DateUtils {

    /**
     * 秒种转时分秒格式时间，例如
     * <pre>
     *      输入38000，输出1小时3分20秒
     *  </pre>
     *
     * @param seconds 秒钟
     * @return 时分秒格式
     */
    public String secondsCovert(int seconds) {
        int hour = seconds / 3600;
        int other = seconds % 3600;
        int minute = other / 60;
        int second = other % 60;
        String time = "";
        if (hour > 0) {
            time += hour + "小时";
        }
        if (minute > 0) {
            time += minute + "分";
        }
        if (second > 0) {
            time += second + "秒";
        }
        return time;
    }

    /**
     * 比较两个时间戳相差毫秒数
     *
     * @param begin 开始时间戳
     * @param end   结束时间戳
     * @return 相差毫秒数
     */
    public Long betweenMillis(Long begin, Long end) {
        LocalDateTime beginLocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(begin), ZoneId.systemDefault());
        LocalDateTime endLocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(end), ZoneId.systemDefault());
        return Duration.between(beginLocalDateTime, endLocalDateTime).toSeconds();
    }
}
